-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Migrate ThreadLocal to ScopedValue usage #27509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
51fcd56 to
b5a6252
Compare
b5a6252 to
4cdc446
Compare
|
Here is a program demonstrating that static void main()
{
ReentrantBoundedExecutor executor = new ReentrantBoundedExecutor(
Executors.newCachedThreadPool(),
1);
executor.execute(() -> {
// This looks rogue, but that can be some other somewhere else using StructuredTaskScope to transport some other ScopedValue to some other place
try (var scope = StructuredTaskScope.open()) {
CyclicBarrier barrier = new CyclicBarrier(2);
var first = scope.fork(() -> {
executor.execute(() -> {
System.out.println("currentThread().getName() = " + currentThread().getName());
try {
barrier.await();
}
catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("two threads are in");
});
});
var second = scope.fork(() -> {
executor.execute(() -> {
System.out.println("currentThread().getName() = " + currentThread().getName());
try {
barrier.await();
}
catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("two threads are in");
});
});
scope.join();
}
catch (Exception e) {
throw new RuntimeException(e);
}
});
} |
| implements Executor | ||
| { | ||
| private final ThreadLocal<Boolean> executorThreadMarkers = ThreadLocal.withInitial(() -> false); | ||
| private final ScopedValue<Void> executorThreadMarkers = ScopedValue.newInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just got to know the usage of it 👍
To be honest, |
Description
Additional context and related issues
Release notes
(x) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text: